$(function(){
        $('#billgrid').edatagrid({
            // url: '../../jquery-easyui-edatagrid/datagrid_data.json',
            data: [],
            singleSelect :true,   //如果为true，则只允许选择一行
            rownumbers:true,     //显示行号
            striped: true,
            fit: true,
            // autoSave: true,
            sortable: true,
            remoteSort: false,
            showFooter: true,
            height: 'auto',
            columns:[[
                {
                    field:'billtypeId',
                    title:'单据类型',
                    width:100,
                    align:'center',
                    editor: {type: "textbox", options: {required: true}}
                },
                {
                    field:'bpgg',
                    title:'规格',
                    width:150,
                    align:'center',
                    editor: {type: "textbox", options: {required: true}}
                },
                {
                    field:'number',
                    title:'件数',
                    width:100,
                    align:'center',
                    editor: {type: "textbox", options: {}}
                },
                {
                    field:'qty',
                    title:'重量',
                    width:100,
                    align:'center',
                    editor: {type: "textbox", options: {}}
                },
                {
                    field:'canagw',
                    title:'仓位',
                    width:100,
                    align:'center',
                    editor: {type: "textbox", options: {}}
                },
                {
                    field:'memo',
                    title:'备注',
                    width:200   ,
                    align:'center',
                    editor: {type: "textbox", options: {}}
                },

            ]],
            onLoadSuccess: function (data) {
                compute();
                $(this).datagrid('resize');
            },
            onSave: function (index, row) {
                compute();
            },
            onDestroy: function (index, row) {
                compute();
            },
            onEdit: function (i, r) {
                index = i;
            },
            onSelect: function (i, d) {
                index = i;
            },
        });

    for (let i = 0; i < 20; i++) {
        $("#billgrid").edatagrid('appendRow', {});
    }


    bindEnterEvents();  //定回车焦点移动到下一个输入文本框中
});

function btn_add() {
    $('#billgrid').edatagrid('saveRow')
    let detailBill = $("#billgrid").datagrid("getRows");
    console.log(detailBill);
}

function compute() {
    let rows = $("#billgrid").edatagrid("getRows");
    let number = 0, qty = 0, total = 0, meter = 0, kilo = 0;
    for (let i = 0; i < rows.length; i++) {
        number += parseFloat(rows[i].number ? rows[i].number : 0);
        qty += parseFloat(rows[i].qty ? rows[i].qty : 0);

    }
    $("#billgrid").edatagrid('reloadFooter', [
        {
            billtypeId: '合计',
            number: number.toFixed(0),
            qty: qty.toFixed(1),
            // meter: meter.toFixed(1),
            // kilo: kilo.toFixed(1),
            // price: price.toFixed(1),
            // total: total.toFixed(1)
        }
    ]);
}

//判断row是否为空
function checkEmptyRow(data) {
    for (let i = 0; i < data.length; i++) {
        if (data[i].billtypeId) {
            return true;
        }
    }
    return false;
}

/* 清行 */
function btn_delete_line() {
    $("#billgrid").edatagrid('destroyRow');
}

/*新增行*/
function btn_add_line() {
    $('#billgrid').edatagrid('addRow');
}

/* 复制 */
function btn_copy_line() {
    $("#billgrid").edatagrid("saveRow");
    let selected = $("#billgrid").edatagrid("getSelected");
    if (selected) {
        selected = JSON.parse(JSON.stringify(selected));
        selected['pairNO'] = '';
        selected['id'] = '';
        selected['pId'] = '';
        $("#billgrid").edatagrid("addRow", {index: index + 1, row: selected});
    } else {
        $.messager.show({title: "提示", msg: "请选择原始行以复制"});
    }
};


function getDate(param) {
    if (isNaN(param)) {
        param = 0;
    }
    var curDate = new Date();
    var newDate = new Date(curDate);
    newDate.setDate(curDate.getDate() + param);
    var year = newDate.getFullYear();
    var month = newDate.getMonth() + 1 < 10 ? '0' + (newDate.getMonth() + 1) : newDate.getMonth() + 1;
    var date = newDate.getDate() < 10 ? '0' + newDate.getDate() : newDate.getDate();
    var separator = '-';
    return year + separator + month + separator + date;
}


//关闭当前标签页事件
function closeTab() {
    window.parent.closeTab();
}